//
//  ZQTextView.m
//  柳州微博
//
//  Created by Apple_Lzzy45 on 16/11/4.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "ZQTextView.h"

@implementation ZQTextView
-(instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        //注册检测文本改变事件
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}
/**
 *  监听文字改变
  */
-(void)textChange
{
    NSLog(@"监听文字改变");
    //消息循环过程中，定期调用
    [self setNeedsDisplay];

}
-(void)setPlacehlder:(NSString *)placeholder
{

    _placeholder=placeholder;
    [self setNeedsDisplay];
    
}
-(void)setPlaceholderColor:(NSString *)placeholderColor
{
    _placeholderColor =placeholderColor;
    [self setNeedsDisplay];

}
-(void)setText:(NSString *)text
{
    [super setText:text];
    [self setNeedsDisplay];
}
-(void)setFont:(UIFont *)font
{
    [super setFont:font];
    [self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
    if (self.hasText) return;

    NSLog(@"drawRect");
    
    CGFloat x =5;
    CGFloat y =8;
    CGFloat w =rect.size.width - 2 *x;
    CGFloat h =rect.size.height -2 * y;
    CGRect placeholderRect =CGRectMake(x, y,w,h);
    
    NSMutableDictionary *attrs=[NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] =self.font;
    
    attrs[NSForegroundColorAttributeName]=self.placeholderColor?self.placeholderColor :[UIColor blackColor];
    [self.placeholder drawInRect:placeholderRect withAttributes:nil];
    
    NSLog(@"drawRect3 =%@",self.placeholder);
    
  
//    [self.placeholder drawInRect:placeholderRect
//                 withAttributes:nil];
}
@end
